import { Options, parseVersion, resolverOptions } from './entity/Options';
import { download } from './utils/download';
import { decompress } from './utils/decompress';
import { copySourceCode } from './utils/copySourceCode';
import { log } from './utils/log';
import { changeAppName } from './utils/changeAppName';
import { moveToOutputFolder } from './utils/moveToOutputFolder';
import { needDecompress } from './utils/changeRunnerFolderName';
import { startRunner } from './utils/startRunner';

export async function nwBuilder(options: Options) {
  log.init(options);
  log.info('构建开始');
  //处理options参数
  options = resolverOptions(options);
  await parseVersion(options);//如果版本是latest/stable/lts则需要解析
  //下载zip包
  await download(options);

  if ('build' === options.mode) {
    //解压zip包
    await decompress(options);
    //复制代码
    await copySourceCode(options);
    //修改应用名
    await changeAppName(options);
    //移动到输出目录
    await moveToOutputFolder(options);
    log.info('构建完成');
  } else if ('run' === options.mode) {
    //判断是否需要解压压缩包
    if (needDecompress(options)) {
      //解压zip包
      await decompress(options);
    }
    //启动nwjs
    await startRunner(options);
  }
}
